package by.apiskun.curex.services.impl;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.apiskun.curex.dataaccess.WalletDAO;
import by.apiskun.curex.datamodel.Wallet;
import by.apiskun.curex.services.WalletService;

@Service
public class WalletServiceImpl implements WalletService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(WalletServiceImpl.class);
	@Inject
	private WalletDAO dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of MoneyService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Wallet get(Long id) {
		Wallet entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Wallet wallet) {
		if (wallet.getId() == null) {
			LOGGER.debug("Save new: {}", wallet);
			dao.insert(wallet);
		} else {
			LOGGER.debug("Update: {}", wallet);
			dao.update(wallet);
		}
	}

	@Override
	public void delete(Wallet wallet) {
		LOGGER.debug("Remove: {}", wallet);
		dao.delete(wallet.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Delete all wallets");
		dao.deleteAll();

	}

	@Override
	public Long getCount() {
		Long count = dao.getCount();
		return count;
	}

	@Override
	public List<Wallet> getAllWalletsByMoneyName(String name) {
		List<Wallet> wallet = dao.getAllWalletsByMoneyName(name);
		return wallet;
	}

	@Override
	public List<Wallet> getAllWalletsByAmount(Double amount) {
		List<Wallet> wallet = dao.getAllWalletsByAmount(amount);
		return wallet;
	}

	@Override
	public List<Wallet> getAllWalletsByAmountRange(Double minAmount,
			Double maxAmount) {
		List<Wallet> wallet = dao.getAllWalletsByAmountRange(minAmount,
				maxAmount);
		return wallet;
	}

	@Override
	public void updateWalletAmount(Long id, Double newAmount) {
		dao.updateWalletAmount(id, newAmount);

	}

	@Override
	public List<Wallet> getAllWallets() {
		List<Wallet> wallets = dao.getAllWalets();
		return wallets;
	}

	@Override
	public List<Wallet> getAllWalletsByUserLogin(String login) {
		List<Wallet> wallet = dao.getAllWalletsByUserLogin(login);
		return wallet;
	}

}
